import pandas as pd
import json
import sys
# 读取 Excel 文件
file_path = r'.\excels\right.xlsx'

# 读取“右侧跟随”sheet为对象数组
right_follow_df = pd.read_excel(file_path, sheet_name='右侧跟随')
right_follow_list = right_follow_df.to_dict(orient='records')  # 转为对象数组

# 读取“部署完成”sheet为对象数组
deploy_done_df = pd.read_excel(file_path, sheet_name='部署完成')
deploy_done_list = deploy_done_df.to_dict(orient='records')  # 转为对象数组

print('执行')

# 打印结果（可选）
# print("右侧跟随对象数组：", right_follow_list)
# print("部署完成对象数组：", deploy_done_list)

# exit()

# 根据名称对比两个数组的数据，如果其中的涨幅超，委托价，计算后股数有不同的话，就放入新数组中，并打印出来
# 创建一个新数组来存储结果
result_list = []  # 存储有差异的数据
missing_list = []  # 存储右侧跟随中有但部署完成中没有的数据

# 遍历右侧跟随数组
for right_follow in right_follow_list:
    found = False  # 标记是否在部署完成中找到匹配项
    # 遍历部署完成数组
    for deploy_done in deploy_done_list:
        # 如果名称相同
        if right_follow['名称'] == deploy_done['名称']:
            found = True
            # 检查三个值的类型是否相同
            if type(right_follow['涨幅超']) != type(deploy_done['涨幅超']):
                print(f"{right_follow['名称']} 的涨幅超类型不同: {type(right_follow['涨幅超'])} vs {type(deploy_done['涨幅超'])}")
                sys.exit()
            if type(right_follow['委托价']) != type(deploy_done['委托价']):
                print(f"{right_follow['名称']} 的委托价类型不同: {type(right_follow['委托价'])} vs {type(deploy_done['委托价'])}")
                sys.exit()
            if type(right_follow['计算后股数']) != type(deploy_done['计算后股数']):
                print(f"{right_follow['名称']} 的计算后股数类型不同: {type(right_follow['计算后股数'])} vs {type(deploy_done['计算后股数'])}")
                sys.exit()
                
            # 检查三个值是否有任意一个不同
            if (right_follow['涨幅超'] != deploy_done['涨幅超'] or 
                right_follow['委托价'] != deploy_done['委托价'] or 
                right_follow['计算后股数'] != deploy_done['计算后股数']):
                print(f"{right_follow['名称']} 有差异:")
                if right_follow['涨幅超'] != deploy_done['涨幅超']:
                    print(f"  涨幅超: {right_follow['涨幅超']} -> {deploy_done['涨幅超']}")
                if right_follow['委托价'] != deploy_done['委托价']:
                    print(f"  委托价: {right_follow['委托价']} -> {deploy_done['委托价']}")
                if right_follow['计算后股数'] != deploy_done['计算后股数']:
                    print(f"  计算后股数: {right_follow['计算后股数']} -> {deploy_done['计算后股数']}")
                result_list.append(right_follow)
            break  # 找到匹配项后跳出内层循环
    
    # 如果在部署完成中没有找到匹配项
    if not found:
        missing_list.append(right_follow)
        print(right_follow['名称'], "在部署完成中未找到")

# 打印结果
# print("结果数组：", result_list)
# 将result_list写入diffrent.json中
with open('diffrent.txt', 'w', encoding='utf-8') as f:
    # 只提取名称字段并用空格连接
    names = ' '.join([item['名称'] for item in result_list])
    f.write(names)

# 将missing_list写入missing.txt中
with open('missing.txt', 'w', encoding='utf-8') as f:
    # 只提取名称字段并用空格连接
    names = ' '.join([item['名称'] for item in missing_list])
    f.write(names)




